﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Utils;
using System.Data;
using Model;
using System.Data.SqlClient;

namespace Dao
{
    public class AuctionSessionDao
    {
        DBConnection dbConnect;
        public AuctionSessionDao()
        {
            dbConnect = new DBConnection();
        }

        public DataTable getAllAuctionSession()
        {
            string sql = "select * from AuctionSession";
            return dbConnect.executeSelectQuery(sql);
        }

        public DataTable getAllAuctionSessionByUser(string userid)
        {
            string sql = @"select * from AuctionSession a join
                            Items i on i.ItemId = a.ItemId where BuyerId =" + userid + "";
            return dbConnect.executeSelectQuery(sql);
        }

        public DataTable getAuctionSessionById(int id)
        {
            string sql = "select * from AuctionSession where sessionId =" + id + "";
            return dbConnect.executeSelectQuery(sql);
        }

        public DataTable checkBuy(string itemId , string userId)
        {
            string sql = "select * from AuctionSession where ItemId =" + itemId + " and BuyerId ="+userId+"";
            return dbConnect.executeSelectQuery(sql);
        }

        public bool insertAuctionSession(AuctionSession auctionSession)
        {
            string query = string.Format(@"insert into AuctionSession 
                (itemId , buyerId , priceOffer , isWinner) 
                values (@itemId ,@buyerId ,@priceOffer ,@isWinner)");
            SqlParameter[] sqlParameters = new SqlParameter[4];

            sqlParameters[0] = new SqlParameter("@itemId", SqlDbType.Int);
            sqlParameters[0].Value = Convert.ToString(auctionSession.ItemId);

            sqlParameters[1] = new SqlParameter("@buyerId", SqlDbType.Int);
            sqlParameters[1].Value = Convert.ToString(auctionSession.BuyerId);

            sqlParameters[2] = new SqlParameter("@priceOffer", SqlDbType.NVarChar);
            sqlParameters[2].Value = Convert.ToString(auctionSession.PriceOffer);

            sqlParameters[3] = new SqlParameter("@isWinner", SqlDbType.NVarChar);
            sqlParameters[3].Value = Convert.ToString(auctionSession.IsWinner);

            return dbConnect.executeInsertQuery(query, sqlParameters);
        }

        public bool updateAuctionSession(AuctionSession auctionSession)
        {
            string query = string.Format(@"update AuctionSession 
                set itemId =@itemId , buyerId =@buyerId , priceOffer =@priceOffer , isWinner =@isWinner
                where sessionId =@sessionId");
            SqlParameter[] sqlParameters = new SqlParameter[5];

            sqlParameters[0] = new SqlParameter("@itemId", SqlDbType.Int);
            sqlParameters[0].Value = Convert.ToString(auctionSession.ItemId);

            sqlParameters[1] = new SqlParameter("@buyerId", SqlDbType.Int);
            sqlParameters[1].Value = Convert.ToString(auctionSession.BuyerId);

            sqlParameters[2] = new SqlParameter("@priceOffer", SqlDbType.NVarChar);
            sqlParameters[2].Value = Convert.ToString(auctionSession.BuyerId);

            sqlParameters[3] = new SqlParameter("@isWinner", SqlDbType.NVarChar);
            sqlParameters[3].Value = Convert.ToString(auctionSession.BuyerId);

            sqlParameters[4] = new SqlParameter("@sessionId", SqlDbType.NVarChar);
            sqlParameters[4].Value = Convert.ToString(auctionSession.SessionId);

            return dbConnect.executeUpdateQuery(query, sqlParameters);
        }

        public bool deleteAuctionSession(int id)
        {
            string query = string.Format("delete AuctionSession  where sessionId =@id");
            SqlParameter[] sqlParameters = new SqlParameter[1];

            sqlParameters[0] = new SqlParameter("@id", SqlDbType.Int);
            sqlParameters[0].Value = Convert.ToString(id);

            return dbConnect.executeDeleteQuery(query, sqlParameters);
        }

    }
}
